﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
#include <QPen>
#include <QFile>
#include <QMessageBox>
#include <qDebug>
#include <math.h>
#include <QFileDialog>
#include <QDir>
#include <QFileInfoList>
#include <QStringListModel>
#include <QScreen>
#include <QCursor>
#include <QPoint>

#define WIDTH 1280
#define HEIGHT 720

quint8 rawImg[HEIGHT][WIDTH][3];

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    pPixmap = new QPixmap(WIDTH,HEIGHT);
    ui->progressBar->hide();
    ui->label_showPixMap->setScaledContents(true);
    //this->setcSetCapture(this);

    QObject::connect ( ui->label_showPixMap, SIGNAL(mouse_pressed(QPoint, QColor)),
                        this, SLOT(on_label_showPixMap_pressed(QPoint, QColor)) );

}

MainWindow::~MainWindow()
{
    delete pPixmap;
    delete ui;
}



void adjust_color_brightness(QColor &color, int percent)
{//percent%
    int Y = color.value();
    Y = Y * percent / 100;

    if(Y >= 255)
        Y = 255;
    if(Y <= 0)
        Y = 0;
    color.setHsv(color.hue(), color.saturation(), Y);
}
void set_RGB_to_BlackWhite(QColor &color)//HSV <=> UVY
{//
    int Y = 0.299 * color.red() + 0.587 * color.green() + 0.114 * color.blue();//
    color.setRgb(Y, Y, Y);
}
void adjust_color_RGB_gain(QColor &color, int Rgain, int Ggain, int Bgain)
{
    int R = color.red() * Rgain / 100;
    int G = color.green() * Ggain / 100;
    int B = color.blue() * Bgain / 100;

    if(R >= 255) R = 255;
    if(G >= 255) G = 255;
    if(B >= 255) B = 255;

    if(R <= 0) R = 0;
    if(G <= 0) G = 0;
    if(B <= 0) B = 0;

    color.setRgb(R, G ,B);
}
void MainWindow::on_pushButtonDraw_clicked()
{
    QPainter painter(pPixmap);             //
    QPen pen;
    QPoint point;
    QColor color;

    ui->pushButtonDraw->setEnabled(false);
    ui->progressBar->show();

    int row, col;
    for(row = 0;row < HEIGHT; row++)//HEIGHT
    {
        QCoreApplication::processEvents(QEventLoop::AllEvents, 5);
        ui->progressBar->setValue(row*100/(HEIGHT-1));
        //qDebug()<<"row = "<<row;
        for(col = 0;col < WIDTH; col++)//WIDTH
        {
            point.setX(col);
            point.setY(row);

            color.setRed(rawImg[row][col][0]);
            color.setGreen(rawImg[row][col][1]);
            color.setBlue(rawImg[row][col][2]);            

            adjust_color_RGB_gain(color,
                                  ui->horizontalSlider_Rgain->value(),
                                  ui->horizontalSlider_Ggain->value(),
                                  ui->horizontalSlider_Bgain->value() );
            if(ui->horizontalSlider_bright->value() != 100)
            {
                adjust_color_brightness(color, ui->horizontalSlider_bright->value());
            }
            if(ui->checkBoxBlackWhite->isChecked())
            {
                set_RGB_to_BlackWhite(color);
            }
            pen.setColor(color);           //
            painter.setPen(pen);           //
            painter.drawPoint(point);
        }
        //ui->label_showPixMap->setPixmap(*pPixmap);
    }
    ui->progressBar->hide();

    ui->label_showPixMap->setPixmap(*pPixmap);
    ui->pushButtonDraw->setEnabled(true);

}


void MainWindow::on_horizontalSlider_actionTriggered(int action)
{
    action = action;
    int k = ui->horizontalSlider->value();
    ui->label_k->setText(QString("缩放%1%").arg(k));
    ui->label_showPixMap->setMaximumSize(QSize(k * WIDTH /100, k * HEIGHT /100));

    int real_width = ui->label_showPixMap->width();
    int real_height = ui->label_showPixMap->height();
    ui->labelSize->setText(QString("%1,%2").arg(real_width).arg(real_height));
}

void MainWindow::on_horizontalSlider_bright_actionTriggered(int action)
{
    action = action;//避免警告
    ui->label_brightness->setText(QString("亮度%1%").arg(ui->horizontalSlider_bright->value()));
}
void MainWindow::on_horizontalSlider_Rgain_valueChanged(int value)
{
    ui->label_Rgain->setText(QString("R %1%").arg(value));
}

void MainWindow::on_horizontalSlider_Ggain_valueChanged(int value)
{
    ui->label_Ggain->setText(QString("G %1%").arg(value));
}

void MainWindow::on_horizontalSlider_Bgain_valueChanged(int value)
{
    ui->label_Bgain->setText(QString("B %1%").arg(value));
}

void MainWindow::on_pushButton_resetRGB_clicked()
{
    ui->horizontalSlider_Rgain->setValue(100);
    ui->horizontalSlider_Ggain->setValue(100);
    ui->horizontalSlider_Bgain->setValue(100);


}


void MainWindow::on_pushButtonSelectFolder_clicked()
{
    QString init_dir = ui->textEditPath->toPlainText();
    init_dir.replace("file:///","");
    ui->textEditPath->setPlainText(init_dir);

    QString dirName = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
                                                         init_dir,
                                                         QFileDialog::ShowDirsOnly
                                                         | QFileDialog::DontResolveSymlinks);
    if("" == dirName)
        return;
    ui->textEditPath->setPlainText(dirName);
    QDir dir(dirName);
    QStringList file_list = dir.entryList(QStringList("*"),QDir::Files);
    ui->listWidget->clear();
    if(file_list.isEmpty())
    {
        QMessageBox::information(NULL, "error", "folder you selected contains 0 file!");
        return;
    }
    else
    {
        ui->listWidget->addItems(file_list);
        ui->listWidget->setCurrentRow(0);
    }


}


void MainWindow::on_listWidget_clicked(const QModelIndex &index)
{
    //QModelIndex avoid = index;//avoid warning
    //QDataStream
    QString dir = ui->textEditPath->toPlainText();
    dir.replace("file:///","");
    ui->textEditPath->setPlainText(dir);

    if(ui->listWidget->currentItem()->text().isEmpty())
    {
        QMessageBox::information(NULL, "error", "please select a file");
        return;
    }

    QString path = dir + "\\" +ui->listWidget->currentItem()->text();
    QFile  file(path);

    if(file.exists())
    {
        qDebug()<<"file exist";
    }
    else
    {
        QMessageBox::information(this, tr("error"), tr("file not exist: ") + path );
        return;
    }
    if(!file.open(QIODevice::ReadOnly))
    {
        QMessageBox::information(this, tr("error"), tr("file open failed: ") + path );
        return;
    }
    else
    {
        qDebug()<<"open success";
    }

    int real_num=0;
    real_num = file.read((char *)rawImg, WIDTH*HEIGHT*3);


    qDebug()<<"real_num = "<<real_num;
}
void MainWindow::on_label_showPixMap_pressed(QPoint mouseLocalPos, QColor pointColor)
{
    //qDebug()<<"鼠标位置:"<<mouseLocalPos;
    //qDebug()<<"鼠标颜色:"<<pointColor;
    ui->labelMousePos->setText( QString("列=%1, 行=%2   R=%3, G=%4, B=%5").arg(mouseLocalPos.x())
                                .arg(mouseLocalPos.y())
                                .arg(pointColor.red())
                                .arg(pointColor.green())
                                .arg(pointColor.blue())  );

}
